package com.sgb.kjwl.viewmodel.enterprise;

import android.app.Application;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.sgb.kjwl.api.APIManager;

import com.sgb.kjwl.model.entity.enterprise.NeedListItemEntity;
import com.sgb.link.utils.Preferences;
import com.swgk.core.base.model.entity.ListResponse;
import com.swgk.core.util.UserOperating;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class AddNeedListModel extends AndroidViewModel {

    public MutableLiveData<String> find_data = new MutableLiveData<>();
    public MutableLiveData<List<NeedListItemEntity>> list_trande = new MutableLiveData<>();
    public MutableLiveData<List<NeedListItemEntity>> list_service = new MutableLiveData<>();
    public MutableLiveData<Integer> show_loading = new MutableLiveData<>();
    public int pagenum_trand = 1;
    public int pagenum_service = 1;
    public int index = 0;
    public MutableLiveData<NeedListItemEntity> updateValue = new MutableLiveData<>();
    public boolean isfind_trande = false;
    public boolean isfind_service = false;
    public NeedListItemEntity data = null;
    public AddNeedListModel(@NonNull Application application) {
        super(application);
    }


    public void getList_Trande(Boolean isRefresh, Map<String,Object> map){
        show_loading.postValue(1);
        pagenum_trand = isRefresh ? 1 : pagenum_trand + 1;
        map.put("limit",20);
        map.put("pageSize",pagenum_trand+"");
        map.put("realName", SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.REAL_NAME,""));
        map.put("userAccount", Preferences.getUserAccount());
        map.put("userNo","0");
        map.put("companyNo","0");
        map.put("teamNo","0");
        map.put("publishType",UserOperating.getInstance().isCompany()?1:UserOperating.getInstance().isTeam()?2:0);
        switch ((Integer) map.get("publishType")){
            case 1:
                map.put("companyNo", SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.COMPANY_NO,""));
                break;
            case 2:
                map.put("teamNo",SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.COMPANY_NO,""));
                break;

            case 0:
                map.put("userNo",SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.USER_NO,""));
                break;
        }
//        if(isfind_trande&&!TextUtils.isEmpty(find_data.getValue())&&index==0) {
//            map.put("title", find_data.getValue());
//        }
        APIManager.getInstance().getNeedAPI().getListInfoByPersonalAddNeed(map).enqueue(new Callback<BaseEntity<ListResponse<List<NeedListItemEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<NeedListItemEntity>>>> call, Response<BaseEntity<ListResponse<List<NeedListItemEntity>>>> response) {
                show_loading.postValue(2);
                if(response!=null&&response.body()!=null&&response.body().getData()!=null&&response.body().getData().getList()!=null){
                    list_trande.postValue(response.body().getData().getList());
                }else{
                    list_trande.postValue(new ArrayList<>());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<NeedListItemEntity>>>> call, Throwable t) {
                show_loading.postValue(2);

            }
        });


    }


    public void getList_service(Boolean isRefresh){
        show_loading.postValue(1);
        pagenum_service = isRefresh ? 1 : pagenum_service + 1;

        Map<String,Object> map = new HashMap<>();
        map.put("limit",20);
        map.put("pageSize",pagenum_service+"");
        map.put("demandType",1);
        if(isfind_service&&!TextUtils.isEmpty(find_data.getValue())&&index==1) {
            map.put("title", find_data.getValue());
        }     map.put("realName", SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.REAL_NAME,""));
        map.put("userAccount", Preferences.getUserAccount());
        map.put("userNo","0");
        map.put("companyNo","0");
        map.put("teamNo","0");
        map.put("publishType",UserOperating.getInstance().isCompany()?1:UserOperating.getInstance().isTeam()?2:0);
        switch ((Integer) map.get("publishType")){
            case 1:
                map.put("companyNo",UserOperating.getInstance().getUserInfo().getCompanyNo());
                break;
            case 2:
                map.put("teamNo",UserOperating.getInstance().getUserInfo().getTeamRespVO()!=null?UserOperating.getInstance().getUserInfo().getTeamRespVO().getNo():"0");
                break;

            case 0:
                map.put("userNo",UserOperating.getInstance().getUserInfo().getUserNo());
                break;
        }
        APIManager.getInstance().getNeedAPI().getListInfoByPersonalAddNeed(map).enqueue(new Callback<BaseEntity<ListResponse<List<NeedListItemEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<NeedListItemEntity>>>> call, Response<BaseEntity<ListResponse<List<NeedListItemEntity>>>> response) {
                show_loading.postValue(2);
                if(response!=null&&response.body()!=null&&response.body().getData()!=null&&response.body().getData().getList()!=null){
                    list_service.postValue(response.body().getData().getList());
                }else{
                    list_service.postValue(new ArrayList<>());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<NeedListItemEntity>>>> call, Throwable t) {
                show_loading.postValue(2);

            }
        });

    }

    public void UpdteItemType(){
        show_loading.postValue(1);
        data.setUi_index(index);
        data.setDemandStatus(data.getDemandStatus().equals("1")?"-1":"1");
        APIManager.getInstance().getNeedAPI().updateNeedMessageType(data.getId(),data.getDemandStatus()).enqueue(new Callback<BaseEntity<Object>>() {
            @Override
            public void onResponse(Call<BaseEntity<Object>> call, Response<BaseEntity<Object>> response) {
                show_loading.postValue(2);
                if(response.body()!=null){
                    updateValue.postValue(data);
                }else{

                }

            }

            @Override
            public void onFailure(Call<BaseEntity<Object>> call, Throwable t) {
                show_loading.postValue(2);

            }
        });

    }





}
